package com.ereport.dao;

import java.sql.Timestamp;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.ereport.entity.Executives;
import com.ereport.entity.LoginDet;
import com.ereport.util.ApplicationContext;

public class LoginDetDAO {

static Transaction tx;
	
	private  void endOperations(Session session) {
		
		session.close();
		session=null;
	}

	private  Session startOperations()throws Exception {
		
		Session session = ApplicationContext.getHiberSession().openSession();
		tx=session.beginTransaction();
		return session;
		
	}

	public LoginDet getLastLogin(int id) throws Exception {
		Session session = startOperations();
		Query query = session.createQuery("from LoginDet where exe_id=:id");
		query.setInteger("id", id);
		LoginDet l = (LoginDet) query.uniqueResult();
		endOperations(session);
		return l;
		
	}

	public void setLastLogin(int id, Timestamp timestamp) throws Exception {
		Session session=startOperations();
		String qtr="Update LoginDet set last_login=:time where exe_id=:id";
		Query qry=session.createQuery(qtr);
		qry.setInteger("id", id);
		qry.setTimestamp("time", timestamp);
		qry.executeUpdate();
		session.getTransaction().commit();
		endOperations(session);
		
		
	}

	
}
